package com.jwork.progress.service.ordermqservice;

import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

/**
 * Notes 订单里的队列业务
 * Author smiler
 * Date 2025/06/20 20:24
 */
@Service
@RequiredArgsConstructor
public class OrderMQService {
    private static Logger logger = LoggerFactory.getLogger(OrderMQService.class);
    @Qualifier("orderProducer")
    private final DefaultMQProducer producer;

    /**
     * Notes 订单超时消息发送
     * Author smiler
     * Date 2025/06/20 20:25
     * @param orderId
     * @param delayLevel
     */
    public void sendDelayMsg(String orderId, int delayLevel) {
        try {
            Message message = new Message("order_timeout_topic", orderId.getBytes(StandardCharsets.UTF_8));
            message.setDelayTimeLevel(delayLevel);
            SendResult sendRes = producer.send(message);
            logger.info("订单超时消息发送成功：{}", sendRes);
        } catch (Exception e) {
            logger.error("订单消息发送失败：" + e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
